package com.app.stick.visit.service.impl;

import java.util.Date;
import java.util.List;
import com.app.common.enums.DeleteEnum;
import com.app.common.enums.sick.PdiBindStatusEnum;
import com.app.common.enums.sick.VisitStatusEnum;
import com.app.common.exception.ServiceException;
import com.app.common.utils.SecurityUtils;
import com.app.common.utils.StringUtils;
import com.app.common.utils.bean.BeanUtils;
import com.app.stick.bind.domain.BizPatientDeviceInfo;
import com.app.stick.bind.mapper.BizPatientDeviceInfoMapper;
import com.app.stick.patient.domain.BizPatientRecord;
import com.app.stick.patient.mapper.BizPatientRecordMapper;
import com.app.stick.visit.domain.vo.BizPatientVisitAddOrEditVo;
import com.app.stick.visit.domain.vo.BizPatientVisitListVo;
import com.app.stick.visit.domain.vo.BizPatientVisitQueryVo;
import com.app.system.service.ISysConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.app.common.utils.DateUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.app.stick.visit.mapper.BizPatientVisitMapper;
import com.app.stick.visit.domain.BizPatientVisit;
import com.app.stick.visit.service.IBizPatientVisitService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 患者就诊信息Service业务层处理
 *
 * @author app
 * @date 2023-12-16
 */
@Service
public class BizPatientVisitServiceImpl extends ServiceImpl<BizPatientVisitMapper, BizPatientVisit> implements IBizPatientVisitService
{
    @Resource
    private BizPatientRecordMapper bizPatientRecordMapper;

    @Resource
    private BizPatientVisitMapper bizPatientVisitMapper;

    @Resource
    private BizPatientDeviceInfoMapper bizPatientDeviceInfoMapper;

    @Resource
    private ISysConfigService configService;

    /**
     * 查询患者就诊信息
     *
     * @param visitId 患者就诊信息主键
     * @return 患者就诊信息
     */
    @Override
    public BizPatientVisit selectBizPatientVisitByVisitId(String visitId)
    {
        return bizPatientVisitMapper.selectById(visitId);
    }

    /**
     * 查询患者就诊信息列表
     *
     * @param queryVo 患者就诊信息
     * @return 患者就诊信息
     */
    @Override
    public List<BizPatientVisitListVo> selectBizPatientVisitList(BizPatientVisitQueryVo queryVo)
    {
        return bizPatientVisitMapper.selectBizPatientVisitList(queryVo);
    }

    /**
     * 新增患者就诊信息
     *
     * @param addOrEditVo 患者就诊信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizPatientVisit(BizPatientVisitAddOrEditVo addOrEditVo)
    {
        //先查看是否存在患者档案 -- 根据身份证号进行判断
        BizPatientRecord patientRecord = bizPatientRecordMapper.selectOne(new LambdaQueryWrapper<BizPatientRecord>()
                .eq(BizPatientRecord::getPatientCard, addOrEditVo.getPatientCard())
                .eq(BizPatientRecord::getDelFlag, DeleteEnum.NO.getCode()));
        if (StringUtils.isNull(patientRecord)) {
            patientRecord = new BizPatientRecord();
            BeanUtils.copyBeanProp(patientRecord, addOrEditVo);
            patientRecord.setDelFlag(DeleteEnum.NO.getCode());
            patientRecord.setCreateTime(new Date());
            patientRecord.setCreateBy(SecurityUtils.getUserId());
            patientRecord.setUpdateTime(new Date());
            patientRecord.setUpdateBy(SecurityUtils.getUserId());
            int recordAdd = bizPatientRecordMapper.insert(patientRecord);
            if (recordAdd <= 0) {
                throw new ServiceException("患者基础档案新增失败");
            }
        } else {
            patientRecord.setPatientName(addOrEditVo.getPatientName());
            patientRecord.setPatientSex(addOrEditVo.getPatientSex());
            patientRecord.setPatientPhone(addOrEditVo.getPatientPhone());
            patientRecord.setUpdateTime(new Date());
            patientRecord.setUpdateBy(SecurityUtils.getUserId());
            int recordEdit = bizPatientRecordMapper.updateBizPatientRecord(patientRecord);
            if (recordEdit <= 0) {
                throw new ServiceException("患者基础档案更新失败");
            }
        }
        BizPatientVisit bizPatientVisit = new BizPatientVisit();
        BeanUtils.copyBeanProp(bizPatientVisit, addOrEditVo);
        //获取就诊号
        String visitCode = configService.selectInpatientCodeByKey();
        if (StringUtils.isEmpty(visitCode)) {
            throw new ServiceException("未获取到患者就诊号");
        }
        bizPatientVisit.setPatientId(patientRecord.getPatientId());
        bizPatientVisit.setVisitCode(visitCode);
        bizPatientVisit.setVisitStatus(VisitStatusEnum.IN.getCode());
        bizPatientVisit.setDelFlag(DeleteEnum.NO.getCode());
        bizPatientVisit.setCreateBy(SecurityUtils.getUserId());
        bizPatientVisit.setCreateTime(DateUtils.getNowDate());
        bizPatientVisit.setUpdateBy(SecurityUtils.getUserId());
        bizPatientVisit.setUpdateTime(DateUtils.getNowDate());
        int visitAdd = bizPatientVisitMapper.insert(bizPatientVisit);
        if (visitAdd <= 0){
            throw new ServiceException("患者就诊信息新增失败");
        }
        return visitAdd;
    }

    /**
     * 修改患者就诊信息
     *
     * @param addOrEditVo 患者就诊信息
     * @return 结果
     */
    @Override
    public int updateBizPatientVisit(BizPatientVisitAddOrEditVo addOrEditVo)
    {
        //先查看是否存在患者档案 -- 根据身份证号进行判断
        BizPatientRecord patientRecord = bizPatientRecordMapper.selectOne(new LambdaQueryWrapper<BizPatientRecord>()
                .eq(BizPatientRecord::getPatientCard, addOrEditVo.getPatientCard())
                .eq(BizPatientRecord::getDelFlag, DeleteEnum.NO.getCode()));
        if (StringUtils.isNull(patientRecord)) {
            throw new ServiceException("未查询到患者的基础档案信息");
        }
        patientRecord.setPatientName(addOrEditVo.getPatientName());
        patientRecord.setPatientSex(addOrEditVo.getPatientSex());
        patientRecord.setPatientPhone(addOrEditVo.getPatientPhone());
        patientRecord.setUpdateTime(new Date());
        patientRecord.setUpdateBy(SecurityUtils.getUserId());
        int recordEdit = bizPatientRecordMapper.updateBizPatientRecord(patientRecord);
        if (recordEdit <= 0) {
            throw new ServiceException("患者基础档案更新失败");
        }
        BizPatientVisit bizPatientVisit = new BizPatientVisit();
        BeanUtils.copyBeanProp(bizPatientVisit, addOrEditVo);
        bizPatientVisit.setUpdateBy(SecurityUtils.getUserId());
        bizPatientVisit.setUpdateTime(DateUtils.getNowDate());
        int visitEdit = bizPatientVisitMapper.updateBizPatientVisit(bizPatientVisit);
        if (visitEdit <= 0){
            throw new ServiceException("患者就诊信息修改失败");
        }
        return visitEdit;
    }

    /**
     * 删除患者就诊信息信息
     *
     * @param visitIds 患者就诊信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBizPatientVisitByVisitIds(String[] visitIds)
    {
        for (String visitId : visitIds) {
            //判断该患者是否已经被绑定
            long patientBindNum = bizPatientDeviceInfoMapper.selectCount(new LambdaQueryWrapper<BizPatientDeviceInfo>()
                    .eq(BizPatientDeviceInfo::getVisitId, visitId)
                    .eq(BizPatientDeviceInfo::getPdiBindStatus, PdiBindStatusEnum.BIND.getCode())
                    .eq(BizPatientDeviceInfo::getDelFlag, DeleteEnum.NO.getCode()));
            if (patientBindNum > 0) {
                throw new ServiceException("该患者已经绑定设备,不能删除");
            }
            BizPatientVisit bizPatientVisit = new BizPatientVisit();
            bizPatientVisit.setVisitId(visitId);
            bizPatientVisit.setDelFlag(DeleteEnum.YES.getCode());
            bizPatientVisit.setUpdateBy(SecurityUtils.getUserId());
            bizPatientVisit.setUpdateTime(new Date());
            int deleteVisit = bizPatientVisitMapper.updateById(bizPatientVisit);
            if (deleteVisit <= 0) {
                throw new ServiceException("患者就诊删除失败");
            }
        }
        return 1;
    }

    /**
     * 删除患者就诊信息信息
     *
     * @param visitId 患者就诊信息主键
     * @return 结果
     */
    @Override
    public int deleteBizPatientVisitByVisitId(String visitId)
    {
        //判断该患者是否已经被绑定
        long patientBindNum = bizPatientDeviceInfoMapper.selectCount(new LambdaQueryWrapper<BizPatientDeviceInfo>()
                .eq(BizPatientDeviceInfo::getVisitId, visitId)
                .eq(BizPatientDeviceInfo::getPdiBindStatus, PdiBindStatusEnum.BIND.getCode())
                .eq(BizPatientDeviceInfo::getDelFlag, DeleteEnum.NO.getCode()));
        if (patientBindNum > 0) {
            throw new ServiceException("该患者已经绑定设备,无需二次绑定");
        }

        BizPatientVisit bizPatientVisit = new BizPatientVisit();
        bizPatientVisit.setVisitId(visitId);
        bizPatientVisit.setDelFlag(DeleteEnum.YES.getCode());
        bizPatientVisit.setUpdateBy(SecurityUtils.getUserId());
        bizPatientVisit.setUpdateTime(new Date());
        return bizPatientVisitMapper.updateById(bizPatientVisit);
    }
}
